package com.hyd.hx_adr.aspect;

import com.hyd.hx_adr.domain.info.BaseInfo;
import com.hyd.hx_adr.utils.Sm4Utils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

@RestControllerAdvice
public class RequestBodyDecodeAdvice extends RequestBodyAdviceAdapter {

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
                                           Class<? extends HttpMessageConverter<?>> converterType) throws IOException {


        String string1 = IOUtils.toString(inputMessage.getBody());
        if (string1 == null) {
            return inputMessage;
        }
        String substring = string1.substring(1, string1.length() - 1);

        try {
            String decrypt = Sm4Utils.decode(substring);
            final ByteArrayInputStream bais = new ByteArrayInputStream(decrypt.getBytes(StandardCharsets.UTF_8));
            return new HttpInputMessage() {
                @Override
                public InputStream getBody() throws IOException {
                    return bais;
                }

                @Override
                public HttpHeaders getHeaders() {
                    return inputMessage.getHeaders();
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.beforeBodyRead(inputMessage, parameter, targetType, converterType);

    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            // 处理请求属性为空的情况，例如抛出异常或记录日志
            throw new RuntimeException("ServletRequestAttributes is null");
        }
        HttpServletRequest request = requestAttributes.getRequest();
        // 获取参数的类型
        Class<?> parameterType = parameter.getParameterType();
        // 判断参数类型是否为 BaseInfo 或其派生类
        if (BaseInfo.class.isAssignableFrom(parameterType) && body instanceof BaseInfo) {
            try {
                ((BaseInfo) body).setOwner((Integer) request.getAttribute("owner"));
            } catch (NumberFormatException e) {
                // 处理 owner 属性转换异常，例如日志记录或抛出更具体的异常
                e.printStackTrace();
            }
        }
        return body;
    }
}
